ajajform_htmltemplates.image_upload={
    htmltemplate:  '<div class="af-label"></div>'+
                    '<div class="af-button">'+            
                        '<div class="button-emul upload active">'+
                            '<div class=label>загрузить</div>'+
                            '<input class="upload_button errored" type="file" name="img_file">'+
							'<div class="cl"></div>'+
                        '</div>'+            
                        '<div class="button-emul status" style="display: none;">'+
                            '<div class=label>0%</div>'+
                        '</div>'+
                        '<div class="button-emul revert active" style="display: none;">'+
                            '<div class=label>отменить</div>'+
                        '</div>'+
                        '<div class="af-error"></div>'+
                    '</div>'+
                    '<div class="cl"></div>'
};
/**
 * Class: ui.ajajform_image_upload
 *		
 */
(function( $, undefined ) {

$.widget("ui.ajajform_image_upload",$.ui.ajajform_widget, {     
	options: {
            cssclass: "afw-upload",
            label: "",
            value:"",
            name:'',// name of image
            url:'', // post to this url
            success:null,
            params:{},
			rparams:{}
	},
	_create: function() {
            this.genericCreate();
            this.refreshLabel();
            this.refreshUpload();
	},
	postvalue: function(r) {
		console.log(this.options.value);
	    r[this.options.name]=this.options.value;
	},
	refreshUpload: function() {
		var t = this;                    
		var formData = t.options.params;
		formData.name = t.options.name;
		
		var widget = t.element;
		var status_label = $('.status .label', widget);

		var on_error = function (e, data, ex, custom) {
			/*
			if (ex) console.log(ex);
			console.log('error e: ' + JSON.stringify(e));
			console.log('error data: ' + JSON.stringify(data));
			console.log('error ex: ' + JSON.stringify(ex));
			*/
			status_label.text('ошибка');            
			setTimeout(function(){
				$('.status', widget).hide();
				$('.upload', widget).show();
			}, 5000);

			if (t.options.error) {
				t.options.error(custom ? ex : e.responseText);
			} else {
				std_response(custom ? ex : e.responseText);
			}
		}

		if (t.options.button_label) {
			$('.af-button .button-emul.upload .label').text(t.options.button_label);
		}
		
		$('.button-emul.upload', widget).fileupload({
			url: t.options.url,
			formData: formData,
			dataType: 'json',
			done: function (e, data) {

				try {
					if(data.result.form_errors && data.result.form_errors[t.options.name]) {
						throw data.result.form_errors[t.options.name];
					}
					if (t.options.success) t.options.success(data.result);
					else std_response(data.result);
				} catch (ex) {
					return on_error(e, data, ex, true);
				}

				if (!t.options.hide_revert) {
					$('.revert', widget).show();
				}
				$('.af-error', widget).html("");
				//console.log('done');
				status_label.text('готово');            
				setTimeout(function(){
						//console.log('done timeout');
						$('.status', widget).hide();
						$('.upload', widget).show();
					}, 2000);            
			},
			error: on_error,
			progressall: function (e, data) {
				var status = parseInt(data.loaded / data.total * 100, 10);
				if (status > 95) status = 95;
				status_label.text(status + '%');
			},
			start: function (e, data) {
				status_label.text('0%');
				$('.upload', widget).hide();
				$('.status', widget).show();
			},
		}).prop('disabled', !$.support.fileInput).parent().addClass($.support.fileInput ? undefined : 'disabled');            
		$('.revert', widget).click(function(){
			if (t.options.success) t.options.success(t.options.rparams);
			else std_response(t.options.rparams);
			$(this).hide();
		}); 
	}
});

})( jQuery );
